#include "delay.h"
#include "stm32f10x.h"

static u8  fac_us=0;
static u16 fac_ms=0;

void delay_init()
{
	
#ifdef OS_CRITICAL_METHOD 	
    u32 reload;
#endif
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);	
    fac_us=SystemCoreClock/8000000;	

#ifdef OS_CRITICAL_METHOD 	
    reload=SystemCoreClock/8000000;	
    reload*=1000000/OS_TICKS_PER_SEC;
  
    fac_ms=1000/OS_TICKS_PER_SEC;
    SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk;   	
    SysTick->LOAD=reload; 	
    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk;   	
#else
    fac_ms=(u16)fac_us*1000;
#endif
}



void delay_us(uint32_t nus)
{
    u32 temp;
    SysTick->LOAD=nus*fac_us; 
    SysTick->VAL=0x00;        
    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;       
    do
    {
        temp=SysTick->CTRL;
    }
    while(temp&0x01&&!(temp&(1<<16)));
    SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;       
    SysTick->VAL =0X00;       
}


void delay_ms(uint16_t nms)
{
    u32 temp;
    SysTick->LOAD=(u32)nms*fac_ms;
    SysTick->VAL =0x00;          
    SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ;        
    do
    {
        temp=SysTick->CTRL;
    }
    while(temp&0x01&&!(temp&(1<<16)));
    SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;     
    SysTick->VAL =0X00;      
}
































